package com.ld.customer.dao;

import com.ld.customer.entity.Customer;
import com.ld.customer.utility.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao1 {

    private static  Connection conn = null;
    private static CustomerDao1 cd;

    public CustomerDao1() {

    }

    public static CustomerDao1 getCd() {
        cd = new CustomerDao1();
        return cd;
    }
    public String getUpdateSql(){
        String sql = "update customer set name = ?, gender = ?, age = ?,phone = ?, email = ? where id = ?";
        return sql;
    }
    public String getDeleteSql(){
        String sql = "delete from customer where id = ?";
        return sql;
    }
    public String getSelectAllSql(){
        String sql = "select * from customer";
        return sql;
    }

    public String getInsertSql(){
        String sql =  "insert into customer(id,name,gender ,age,phone, email) values(?,?,?,?,?,?)";
        return sql;
    }


    public boolean update(String sql , Object ...args){
        boolean isFlag = true;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            int i = ps.executeUpdate();
            if (i != 1){
                isFlag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.getClose(ps , conn);
        }

        return isFlag;
    }

    public Customer selectById(int id) {
        String sql = "select * from customer where id = ? " ;
        Customer cu = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
             ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
             rs = ps.executeQuery();
            if (rs.next()) {
                cu = new Customer(rs.getString("name"), rs.getString("gender"),
                        rs.getInt("age"), rs.getString("phone"),
                        rs.getString("email"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.getClose(rs,ps,conn);
        }
        return cu;
    }

    public  <T> List<T> getConnection(Class<T> clazz , String sql , Object ...args)  {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
             ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            rs = ps.executeQuery();
            //获取结果集中的元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            //获取列数
            int columnCount = rsmd.getColumnCount();
            ArrayList<T> list = new ArrayList<>();
            while (rs.next()){
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取结果集中的数据
                    Object columnValue = rs.getObject(i + 1);
                    //列值        getColumnLabel 该方法是数据库中属性的名称，如果有别名，获取别名
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    //通过反射设置值
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.getClose(rs,ps,conn);
        }
        return  null;
    }

}
